<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/28
 * Time: 14:21
 */

//商品有个的操作方法

namespace Controller;

use Model\ShopModel;

class ShopController extends TooController{
    private $model; //定义属性，
    public function __construct()
    {
        parent::__construct(); //先执行分类的构造方法
        $this->model=new ShopModel();
    }

    //分类管理
    public function fl(){
        $arr=$this->model->get();
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    //分类修改和删除
    //修改和删除方法
    public function fl_upd(){
        $model=$this->model; //初始化的模型
        if($_POST){
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['sort']=$_POST['sort'];
            if($model->pdo_upd('sh_fl',$arr," where id = '{$_POST['id']}'")){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }

        $type=$_GET['type'];
        $id=$_GET['id'];
        if($type=='del'){
            //删除
            $sql="delete from sh_fl where id in ($id)";
            if($model->pdo_exec($sql)){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }
        else{
            //修改 --- 查询修改的数据
            $list= $model->pdo_sel("select * from sh_fl where id = $id limit 1");

            $this->view->assign('arr',$list);
            $this->view->display('fl_add');
        }
    }

    //分类添加
    public function fl_add(){
        if($_POST){
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['sort']=$_POST['sort'];
            $model=$this->model;
            if($model->pdo_insert('sh_fl',$arr)){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }
        $this->view->display();
    }

    //商品管理
    public function sp(){
        $where="where";
        if(!empty($_POST['stime'])){
            $where.= ' time >'.strtotime($_POST['stime']).' and';
        }
        if(!empty($_POST['etime'])){
            $etime=strtotime($_POST['etime'])+(24*3600);
            $where.= ' time <'.$etime.' and';
        }
        if(!empty($_POST['name'])){
            $where.= " name like '%{$_POST['name']}%' and";
        }
        if(!empty($_POST['stat'])){
            $where.= " stat = '{$_POST['stat']}' and";
        }
        //判断where 是否有条件
        if($where!='where'){
            $where=trim($where,'and');
        }else{
            $where='';
        }

        //查看数据
        $goods=$this->model->pdo_sel("select * from sh_goods $where",2);
        $this->view->assign('goods',$goods);


        $this->view->display();
    }

    //商品添加
    public function sp_add(){
        if($_POST['name']){
            //提交表单
            //存储基本数据
            $arr=array();
            $arr['img']=$_POST['img'];
            $arr['name']=$_POST['name'];
            $arr['kc']=$_POST['kc'];
            $arr['msg']=$_POST['msg'];
            $arr['txt']= htmlspecialchars($_POST['editorValue']); //转义HTML代码
            $arr['stat']= $_POST['stat'];
            $arr['money']= $_POST['money'];
            $arr['f_id']= $_POST['f_id'];
            $arr['c_id']= $_POST['c_id'];
            $arr['time']= time(); //添加时间
            $g_id=$this->model->pdo_insert('sh_goods',$arr);//存入数据库
            if($g_id){
                //存储对应的规格
                //添加的规格名字和规格加价位置是一一对应的
                $sql='insert into sh_goods_g(gname,gmoney,g_id) value ';
                for($i=0;$i<count($_POST['gname']);$i++){
                    $sql.="('{$_POST['gname'][$i]}',{$_POST['gmoney'][$i]},$g_id),";
                }

                $sql=trim($sql,',');
                if($this->model->pdo_exec($sql)){
                    $this->success('添加成功');
                }

            }
            $this->success('添加失败');
        }else{
            //分类
            $fl=$this->model->pdo_sel("select * from sh_fl",2);
            $lm=$this->model->pdo_sel("select * from sh_category",2);
            $this->view->assign('fl',$fl);
            $this->view->assign('lm',$lm);
            $this->view->display();
        }
    }
}